﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using JigLibX.Physics;
using JigLibX.Collision;

namespace LloydGame
{
    public class GamePlayer : GameComponent
    {
        private CarObject m_carObject;
        private int m_points;
        private int m_points_show;
        private NetworkGamer m_gamer;
        protected Texture2D m_hud_playerindicator_left, m_hud_playerindicator_right;
        private List<PowerUpObject> m_activePowerUps;

        #region Properties
        public CarObject carObject
        {
            get { return m_carObject; }
            set { m_carObject = value; }
        }
        public NetworkGamer gamer
        {
            get { return m_gamer; }
            set { m_gamer = value; }
        }
        public int points
        {
            get { return m_points; }
            set { m_points = value; }
        }
        public int show_points
        {
            get { return m_points_show; }
        }
        public List<PowerUpObject> activePowerUps
        {
            get { return m_activePowerUps; }
            set { m_activePowerUps = value; }
        }
        #endregion Properties

        public GamePlayer(Game p_game, CarObject p_carObject) : base(p_game)
        {
            m_carObject = p_carObject;
            m_points = 0;
            m_activePowerUps = new List<PowerUpObject>();
        }

        public GamePlayer(Game p_game, NetworkGamer p_gamer, CarObject p_carObject) : base(p_game)
        {
            m_carObject = p_carObject;
            m_points = 0;
            m_points_show = 0;
            m_gamer = p_gamer;

            m_hud_playerindicator_left = Texture2D.FromFile(Game.GraphicsDevice, "Content\\hud_player_indicator_left.png");
            m_hud_playerindicator_right = Texture2D.FromFile(Game.GraphicsDevice, "Content\\hud_player_indicator_right.png");

            m_activePowerUps = new List<PowerUpObject>();
        }

        public override void Update(GameTime p_gameTime)
        {
            if (m_points_show > m_points)
            {
                m_points_show -= (int)((m_points_show - m_points) / 4.0f);
            }
            else if (m_points_show < m_points)
            {
                m_points_show += (int)((m_points - m_points_show) / 4.0f);
            }

            if (Math.Abs(m_points_show - m_points) < 5)
                m_points_show = m_points;

            if (m_activePowerUps.Count > 0)
            {
                List<PowerUpObject> powerUpsToRemove = new List<PowerUpObject>();
                foreach ( PowerUpObject activePowerUp in m_activePowerUps)
                {
                    if ((((Game1)Game).gameTime.TotalRealTime - activePowerUp.timeTaken) > new TimeSpan(0, 0, 10))
                    {
                        powerUpsToRemove.Add(activePowerUp);
                    }
                    if(activePowerUp.powerUpType == "NITRO")
                    {
                        m_carObject.speed = 1.0f;
                        if ((((Game1)Game).gameTime.TotalRealTime - activePowerUp.timeTaken) > new TimeSpan(0, 0, 10))
                        {
                            m_carObject.speed = 0.5f;
                        }
                        //if(((Game1)Game).gameTime.TotalRealTime() - activePowerUp.timeTaken.TotalRealTime() > )
                        /*if (m_carObject.body.Velocity.Length() < 10)
                        {
                            m_carObject.body.Velocity += Vector3.Transform(Vector3.Right,
                                m_carObject.body.Orientation * m_carObject.skin.GetPrimitiveLocal(0).Transform.Orientation);
                        }*/
                    }
                    if (activePowerUp.powerUpType == "LOW_FRICTION")
                    {
                        m_carObject.car.frictionMult = 0.1f;
                        if ((((Game1)Game).gameTime.TotalRealTime - activePowerUp.timeTaken) > new TimeSpan(0, 0, 10))
                        {
                            m_carObject.car.frictionMult = 1.0f;
                            powerUpsToRemove.Add(activePowerUp);
                        }
                    }
                    if (activePowerUp.powerUpType == "PUNCH")
                    {
                        Random rand = new Random();
                        m_carObject.body.Velocity += Vector3.Transform(Vector3.Up * 40.0f, Matrix.CreateFromYawPitchRoll(0.0f, rand.Next(0, 2), rand.Next(0, 2)));
                        powerUpsToRemove.Add(activePowerUp);
                    }
                    if (activePowerUp.powerUpType == "REVERSE_KEYS")
                    {
                        m_carObject.reverse_keys = true;
                        if ((((Game1)Game).gameTime.TotalRealTime - activePowerUp.timeTaken) > new TimeSpan(0, 0, 10))
                        {
                            m_carObject.reverse_keys = false;
                            powerUpsToRemove.Add(activePowerUp);
                        }
                    }
                    if (activePowerUp.powerUpType == "SLOW")
                    {
                        m_carObject.speed = 0.25f;
                        if ((((Game1)Game).gameTime.TotalRealTime - activePowerUp.timeTaken) > new TimeSpan(0, 0, 10))
                        {
                            m_carObject.speed = 0.5f;
                        }
                    }
                    if (activePowerUp.powerUpType == "DOUBLEYOURPOINTS")
                    {
                        m_points *= 2;
                        powerUpsToRemove.Add(activePowerUp);
                    }
                    if (activePowerUp.powerUpType == "DOUBLETHEIRPOINTS")
                    {
                        m_points *= 2;
                        powerUpsToRemove.Add(activePowerUp);
                    }
                }
                foreach (PowerUpObject powerUpToRemove in powerUpsToRemove)
                {
                    m_activePowerUps.Remove(powerUpToRemove);
                }
            }
        }

        public void DrawGamerTag(SpriteBatch t_spritebatch)
        {
            if (m_gamer != null)
            {
                Vector3 t_renderpos = m_carObject.position;
                Vector2 t_screenpos;

                t_renderpos = Game.GraphicsDevice.Viewport.Project(t_renderpos + Vector3.Up * 3.0f, ((Game1)Game).camera.proj, ((Game1)Game).camera.view, Matrix.Identity);
                t_screenpos.X = t_renderpos.X - (m_gamer.Gamertag.Length * 5);
                t_screenpos.Y = t_renderpos.Y;

                // Color (to fade text)
                Color t_color = Color.White;
                
                    t_spritebatch.Begin(SpriteBlendMode.AlphaBlend);

                    if ((t_screenpos.X > 0.0f) && (t_screenpos.X < Game.GraphicsDevice.Viewport.Width) && (t_renderpos.Z < 1.0f))
                    {
                        // Not outside the window, draw gamertag
                        t_spritebatch.DrawString(((Game1)Game).menuFontTiny, m_gamer.Gamertag, t_screenpos, t_color);
                    }
                    else
                    {
                        //Vector2 t_rotvec = t_screenpos;
                        if (t_screenpos.X < 0.0f)
                            t_screenpos.X = 0.0f;
                        if (t_screenpos.X > Game.GraphicsDevice.Viewport.Width - 32.0f)
                            t_screenpos.X = Game.GraphicsDevice.Viewport.Width - 32.0f;
                        if (t_screenpos.Y < 0.0f)
                            t_screenpos.Y = 0.0f;
                        if (t_screenpos.Y > Game.GraphicsDevice.Viewport.Height - 32.0f)
                            t_screenpos.Y = Game.GraphicsDevice.Viewport.Height - 32.0f;
                        if (t_renderpos.Z >= 1.0f)
                            t_screenpos.Y = Game.GraphicsDevice.Viewport.Height - 32.0f;

                        //t_rotvec = new Vector2(Game.GraphicsDevice.Viewport.Width / 2.0f + t_rotvec.X, Game.GraphicsDevice.Viewport.Height / 2.0f + t_rotvec.Y);

                        // Outside screen
                        if (t_screenpos.X < Game.GraphicsDevice.Viewport.Width / 2.0f)
                            t_spritebatch.Draw(m_hud_playerindicator_left, t_screenpos, t_color);
                        else
                            t_spritebatch.Draw(m_hud_playerindicator_right, t_screenpos, t_color);
                    }
                    t_spritebatch.End();
            }
        }
    }
}
